package com.tang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.reggie.common.Constant;
import com.tang.reggie.exception.CustomException;
import com.tang.reggie.mapper.UserMapper;
import com.tang.reggie.pojo.User;
import com.tang.reggie.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * 前台用户登录
     * @param phone
     * @return
     */
    @Override
    public User login(String phone) {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(phone!=null,User::getPhone,phone);
        User user = super.getOne(userLambdaQueryWrapper);
        if (user==null){
            user = new User();
            user.setPhone(phone);
            user.setStatus(Constant.ENNABLE_CODE);
            super.save(user);
        }
        if (user.getStatus()==0){
            throw new CustomException("用户已禁用");
        }
        return user;
    }
}
